package com.ulook.lookplanet.service;

import com.ulook.lookplanet.bean.*;
import com.ulook.lookplanet.mapper.ArticleMapper;
import com.ulook.lookplanet.util.FileUtil;
import com.ulook.lookplanet.util.mgc.SensitiveUtil;
import com.ulook.lookplanet.util.mgc.SensitiveWordInit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 描述：
 *
 * @author L1998
 * @date 2021/4/16
 */
@Service
public class ArticleService {

    @Autowired
    private ArticleMapper mapper;

    @Autowired
    private SensitiveWordInit sensitiveWordInit;

    /**
     * 获取文章的图片
     */
    public List<TArtPic> getArtPic(String artid){
        return mapper.getArtPic(artid);
    }

    /**
     * 点赞
     */
    @Transactional
    public void like(TLikes likes, boolean isCancel){
        System.out.println("isCancel:  " + isCancel);
        if(isCancel){
            mapper.cancelLike(likes);
        }else {
            if(mapper.hasRecordLike(likes) == 0){
                mapper.like(likes);
                addView(likes.getUserid(), likes.getArtid());
            }
        }
    }

    /**
     * 收藏
     */
    @Transactional
    public void collect(TCollect collect, boolean isCancel){
        if(isCancel){
            mapper.cancelCollect(collect);
        }else {
            if(mapper.hasRecordCollect(collect) == 0){
                mapper.collect(collect);
                addView(collect.getUserid(), collect.getArtid());
            }
        }
    }

    /**
     * 添加浏览记录
     */
    @Transactional
    public void addView(int userid, String artid){
        mapper.addView(userid, artid);
    }

    /**
     * 查询点赞、收藏、评论数量
     */
    public VArtCount getArtCount(String artId){
        return mapper.getArtCount(artId);
    }

    /**
     * 评论
     */
    @Transactional
    public VCommentInfo comment(TComment comment){
        try {
            //将文本中的敏感词读取出来存入List集合
            ClassPathResource classPathResource = new ClassPathResource("/static/maren.txt");
            List<String> list = FileUtil.readFile2List(classPathResource.getFile());
            //初始化词库
            Map sensitiveWordMap = sensitiveWordInit.initSensitiveWord(list);
            //初始化传入SensitiveUtil的敏感词库
            SensitiveUtil.sensitiveWordMap = sensitiveWordMap;
            String content = comment.getContent();
            String re = SensitiveUtil.replaceSensitiveWord(content, 2, "*");
            comment.setContent(re);
        } catch (Exception e) {
            e.printStackTrace();
        }
        mapper.comment(comment);
        return mapper.getCommentByUid(comment.getUid());
    }

    /**
     * 通过uid查询评论
     */
    public VCommentInfo getCommentByUid(String uid){
        return mapper.getCommentByUid(uid);
    }

    /**
     * 获取评论
     */
    public List<VCommentInfo> getComment(String artid, int itemNum){
        return mapper.getComment(artid, itemNum);
    }

    /**
     * 获取话题
     */
    public List<TTopic> getTopicByArtId(String artid){
        return mapper.getTopicByArtId(artid);
    }

    /**
     * 模糊查询
     */
    public List<VArticlePreInfo> searchArt(String s, int userid, int itemNum){
        return mapper.searchArt(s, userid, itemNum);
    }
}
